package com.onenine.app.fragment;

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.onenine.app.R;
import com.onenine.app.event.CommentScrollEvent;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.RoundViewOutlineProvider;
import com.onenine.chat.util.SimpleCommonUtils;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.ScreenUtils;

import org.greenrobot.eventbus.EventBus;

import cn.jzvd.JZCacheProxy;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * 创建时间：2018/7/16 0016 14:23
 * 创建人：fly
 * 功能描述：用户视频帖子内容
 */
public class PostUserVideoContentFragment extends Fragment {

    private JzvdStd mJzvdStd;
    private TextView mTvDesc;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_video_post_content, container, false);
        mJzvdStd = view.findViewById(R.id.jz_video_view);
        mTvDesc = view.findViewById(R.id.tv_content);
        return view;
    }

    /**
     * 绑定数据
     *
     * @param postDesc
     * @param picUrls
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void bindTopicData(final String postDesc, final String picUrls) {
        Looper.myQueue().addIdleHandler(() -> {
            SimpleCommonUtils.spannableEmoticonFilter(mTvDesc, postDesc);
            loadSmallVideo(picUrls.split(","));
            EventBus.getDefault().post(new CommentScrollEvent());
            return false;
        });
    }

    /**
     * 显示小视频
     *
     * @param videos
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void loadSmallVideo(final String[] videos) {
        mJzvdStd.setOutlineProvider(new RoundViewOutlineProvider(ApUtils.getApp().getResources()
                .getDimensionPixelOffset(R.dimen.common_round_corner)));
        mJzvdStd.setClipToOutline(true);
        if (videos.length == 2) {
            mJzvdStd.setUp(JZCacheProxy.getJzCacheProxy(ApUtils.getApp())
                    .getCacheDataSource(videos[1]), Jzvd.SCREEN_WINDOW_NORMAL);
            int videoMaxWidth = ScreenUtils.getScreenWidth() - 2 * getResources().getDimensionPixelOffset(R.dimen.dp25);
            GlideUtils.loadVideoImageFitWidth(ApUtils.getApp(), mJzvdStd.thumbImageView, videos[0]
                    , videoMaxWidth, new GlideUtils.OnFitCallback() {
                        @Override
                        public void callback(Pair<Float, Float> sizePair) {
                            if (sizePair == null) {
                                return;
                            }
                            ViewGroup.LayoutParams lp = mJzvdStd.getLayoutParams();
                            lp.width = sizePair.first.intValue();
                            lp.height = sizePair.second.intValue();
                        }
                    });
        } else if (videos.length == 1) {
            mJzvdStd.setUp(JZCacheProxy.getJzCacheProxy(ApUtils.getApp())
                    .getCacheDataSource(videos[0]), Jzvd.SCREEN_WINDOW_NORMAL);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }

}
